﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BusinessLogic.ProfileManagementLogic;
using Entities;

namespace MockProjectGroupThree.Presentation.ProfileManagement
{
    public partial class UpdateClient : System.Web.UI.Page
    {
        ClientProfileManagementLogic clientProfile=new ClientProfileManagementLogic();
        private Client client = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            LoadUpdateClient();
        }

        private void LoadUpdateClient()
        {
            var id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                var dataTable = clientProfile.GetClientById(id);
                textClientId.Text = id;
                textUserName.Text = dataTable.Rows[0]["UserName"].ToString();
                textCompany.Text = dataTable.Rows[0]["CompanyName"].ToString();
                textAddress.Text = dataTable.Rows[0]["Address"].ToString();
                textPhoneNumber.Text = dataTable.Rows[0]["PhoneNumber"].ToString();
                textEmail.Text = dataTable.Rows[0]["Email"].ToString();
            }
        }

        protected void buttonSave_Click(object sender, EventArgs e)
        {
            client = new Client
            {
                ClientId = int.Parse(textClientId.Text.Trim()),
                UserName = textUserName.Text,
                CompanyName = textCompany.Text,
                Address = textAddress.Text,
                PhoneNumber = int.Parse(textPhoneNumber.Text),
                Email = textEmail.Text
            };
            var result = clientProfile.UpdateClient(client);
            if (result > 0)
            {
                Response.Redirect("ClientManager.aspx");
            }
        }
    }
}
